﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceManager.DataAccessLayer
{
    public class RoomDbOperation:IDatabaseOperation<BOPHAN>
    {
        QLThietBiEntities devicemanager;
        #region IDatabaseOperation<T> Members

        public int SaveRecord(BOPHAN record)
        {
            try
            {
                using (devicemanager = new QLThietBiEntities())
                {
                    devicemanager.BOPHANs.Add(record);
                    devicemanager.SaveChanges();
                }
                return (int)record.BoPhanID;
            }
            catch
            {
                return -1;
            }
        }

        public bool UpdateRecord(BOPHAN record)
        {
            using (devicemanager = new QLThietBiEntities())
            {
                devicemanager.BOPHANs.Find(record.BoPhanID);
                devicemanager.SaveChanges();
                return true;
            }
        }

        public List<BOPHAN> GetRecord(BOPHAN record)
        {
            devicemanager = new QLThietBiEntities();
            IQueryable<BOPHAN> custQuery = devicemanager.BOPHANs.AsQueryable<BOPHAN>();
            return custQuery.ToList();
        }

        public bool DeleteRecord(BOPHAN record)
        {

            using (devicemanager = new QLThietBiEntities())
            {
                if (record.BoPhanID != null)
                {
                    var cust = devicemanager.DONVITINHs.FirstOrDefault(c => c.DVTIT == record.BoPhanID);
                    devicemanager.DONVITINHs.Remove(cust);
                    devicemanager.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        #endregion
    }
}
